package com.bt.lms.dao.servise.impl;

import com.bt.lms.constant.BOConstant;
import com.bt.lms.entity.FamilyMemberInfo;
import com.bt.lms.dao.servise.StuFamilyMemberDao;
import com.bt.lms.utils.DriverUtils;

import java.sql.Connection;
import java.sql.Date;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.time.LocalDate;

public class StuFamilyMemberDaoImpl implements StuFamilyMemberDao {

    @Override
    public int insert(String sql, FamilyMemberInfo familyMemberInfo) throws SQLException {
        //需要手动维护的数据
        //获取学员的id
        //传过来的id

        //获取要添加的家庭成员的学员id
        Connection conn = DriverUtils.Driver();
        //sql语句
        String relation = familyMemberInfo.getRelation();
        String name = familyMemberInfo.getName();
        LocalDate birthday = familyMemberInfo.getBirthday();
        String workUnit = familyMemberInfo.getWorkUnit();
        String job = familyMemberInfo.getJob();
        String phone = familyMemberInfo.getPhone();
        String nation = familyMemberInfo.getNation();
        Integer nationality = familyMemberInfo.getNationality();
        String healthCondition = familyMemberInfo.getHealthCondition();
        String duty = familyMemberInfo.getDuty();
        String dutyRank = familyMemberInfo.getDutyRank();
        String email = familyMemberInfo.getEmail();
        Integer isEmergencyContact = familyMemberInfo.getIsEmergencyContact();
        Long stuBasicInfoID = familyMemberInfo.getStuBasicInfoID();

        PreparedStatement ps = conn.prepareStatement(sql);
        ps.setString(1,relation);
        ps.setString(2,name);
        ps.setDate(3, Date.valueOf(birthday));
        ps.setString(4,workUnit);
        ps.setString(5,job);
        ps.setString(6,phone);
        ps.setString(7,nation);
        ps.setInt(8,nationality);
        ps.setString(9,healthCondition);
        ps.setString(10,duty);
        ps.setString(11,dutyRank);
        ps.setString(12,email);
        ps.setInt(13,isEmergencyContact);
        ps.setLong(14,stuBasicInfoID);


        int i = ps.executeUpdate();
        //关闭资源
        ps.close();
        conn.close();

        return i;
    }

    @Override
    public boolean upData(String sql,FamilyMemberInfo familyMemberInfo,Long id) throws SQLException {

        String relation = familyMemberInfo.getRelation();
        String name = familyMemberInfo.getName();
        LocalDate birthday = familyMemberInfo.getBirthday();
        String workUnit = familyMemberInfo.getWorkUnit();
        String job = familyMemberInfo.getJob();
        String phone = familyMemberInfo.getPhone();
        String nation = familyMemberInfo.getNation();
        Integer nationality = familyMemberInfo.getNationality();
        String healthCondition = familyMemberInfo.getHealthCondition();
        String duty = familyMemberInfo.getDuty();
        String dutyRank = familyMemberInfo.getDutyRank();
        String email = familyMemberInfo.getEmail();
        Integer isEmergencyContact = familyMemberInfo.getIsEmergencyContact();
        Long stuBasicInfoID = familyMemberInfo.getStuBasicInfoID();

        //获取连接
        Connection conn = DriverUtils.Driver();
        PreparedStatement ps = conn.prepareStatement(sql);
        ps.setString(1,relation);
        ps.setString(2,name);
        ps.setDate(3, Date.valueOf(birthday));
        ps.setString(4,workUnit);
        ps.setString(5,job);
        ps.setString(6,phone);
        ps.setString(7,nation);
        ps.setInt(8,nationality);
        ps.setString(9,healthCondition);
        ps.setString(10,duty);
        ps.setString(11,dutyRank);
        ps.setString(12,email);
        ps.setInt(13,isEmergencyContact);
        ps.setLong(14,id);
        ps.setLong(15,stuBasicInfoID);

        int i = ps.executeUpdate();

        //关闭资源
        ps.close();
        conn.close();
        return true;
    }
    @Override
    public boolean delete(String sql,Long id) throws SQLException {
        Connection conn = DriverUtils.Driver();
        PreparedStatement ps = conn.prepareStatement(sql);
        ps.setInt(1, BOConstant.FAMILY_MEMBER_DELETED_YES);
        ps.setInt(2,1);
        ps.setLong(3,id);
        int i = ps.executeUpdate();

        //关闭资源
        ps.close();
        conn.close();
        return false;
    }
}
